package com.bilin.coin.tools;

import android.app.Activity;
import android.content.DialogInterface;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;

import com.bilin.coin.R;


public class AlertUtils {

    public static void dialog(Activity activity, final String title, CharSequence content,OnPositiveClick onPositiveClick) {
        View view = View.inflate(activity,R.layout.dialog_common, null);
        AlertDialog.Builder builder = new AlertDialog.Builder(activity)
                .setTitle("")
                .setView(view);
        AlertDialog alertDialog = builder.create();
        alertDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);//去掉白色背景·
        TextView tv_title = (TextView) view.findViewById(R.id.tv_title);
        TextView tv_content = (TextView) view.findViewById(R.id.tv_content);
        tv_title.setText(title);
        tv_content.setText(content);
        Button bt_confirm = (Button) view.findViewById(R.id.bt_confirm);
        Button bt_cancel = (Button) view.findViewById(R.id.bt_cancel);

        bt_confirm.setOnClickListener(v -> {
            if (onPositiveClick != null) {
                onPositiveClick.onClick();
                alertDialog.dismiss();
            } else {
                alertDialog.dismiss();
            }
        });
        bt_cancel.setOnClickListener(v -> {
            alertDialog.dismiss();
        });

        alertDialog.show();
    }

    public static void dialog(Activity activity, OnPositiveClick onPositiveClick) {
        View view = View.inflate(activity,R.layout.dialog_common_tips, null);
        AlertDialog.Builder builder = new AlertDialog.Builder(activity)
                .setTitle("")
                .setView(view);
        AlertDialog alertDialog = builder.create();
        alertDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);//去掉白色背景·

        ImageView iv_close = (ImageView) view.findViewById(R.id.iv_close);
        Button bt_confirm = (Button) view.findViewById(R.id.bt_confirm);

        bt_confirm.setOnClickListener(v -> {
            if (onPositiveClick != null) {
                onPositiveClick.onClick();
                alertDialog.dismiss();
            } else {
                alertDialog.dismiss();
            }
        });
        iv_close.setOnClickListener(v -> {
            alertDialog.dismiss();
        });

        alertDialog.show();
    }

    public static void dialog(Activity activity, final String title, CharSequence Message,CharSequence confirmString,OnPositiveClick onPositiveClick) {
        new androidx.appcompat.app.AlertDialog.Builder(activity)
                .setTitle(title)//设置对话框的标题
                .setMessage(Message)//设置对话框的内容
                //设置对话框的按钮
                .setNegativeButton(activity.getText(R.string.cancel), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                .setPositiveButton(confirmString, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (onPositiveClick != null) {
                            onPositiveClick.onClick();
                        } else {
                            dialog.dismiss();
                        }
                    }
                }).create().show();
    }

    public static void dialog2(Activity activity, final String title, CharSequence content,OnPositiveClick onPositiveClick) {
        View view = View.inflate(activity,R.layout.dialog_common2, null);
        AlertDialog.Builder builder = new AlertDialog.Builder(activity)
                .setTitle("")
                .setView(view);
        AlertDialog alertDialog = builder.create();
        alertDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);//去掉白色背景·
        TextView tv_title = (TextView) view.findViewById(R.id.tv_title);
        TextView tv_content = (TextView) view.findViewById(R.id.tv_content);
        tv_title.setText(title);
        tv_content.setText(content);
        Button bt_confirm = (Button) view.findViewById(R.id.bt_confirm);
        Button bt_cancel = (Button) view.findViewById(R.id.bt_cancel);

        bt_confirm.setOnClickListener(v -> {
            if (onPositiveClick != null) {
                onPositiveClick.onClick();
                alertDialog.dismiss();
            } else {
                alertDialog.dismiss();
            }
        });
        bt_cancel.setOnClickListener(v -> {
            alertDialog.dismiss();
        });

        alertDialog.show();
    }

    public interface OnPositiveClick{
        void onClick();
    }
}
